﻿using Kaskelot.SP.Core;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;
using Stockholm.SOA.Kaskelot.WCF.FaultContracts;
using Stockholm.SOA.Kaskelot.WCF.ServiceContracts;
using System;
using System.Collections.Generic;
using System.ServiceModel;

namespace Kaskelot.SP.Services
{
	public class PersonService : BaseService<IKaskelotService>
	{
		public IList<PersonSearchResult> SearchPersoner(PersonSearchParameters parameters)
		{
			IList<PersonSearchResult> result = null;
			try
			{
				result = this.channel.SearchPersoner(ApplicationContext.Current.Val.Id, parameters);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "SearchPersoner", "Error searching personer", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}

			return result;
		}

		public Person GetPerson(Guid id)
		{
			Person result = null;
			try
			{
				result = this.channel.GetPerson(ApplicationContext.Current.Val.Id, id);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetPerson", "Error getting person", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}

			return result;
		}

		public Person GetPersonByPersonnummer(string personnummer)
		{
			Person result = null;
			try
			{
				result = this.channel.GetPersonByPersonnummer(ApplicationContext.Current.Val.Id, personnummer);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetPerson", "Error getting person by personnummer", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}

			return result;
		}

		public IEnumerable<PersonFull> GetPersonList(IEnumerable<Guid> idList)
		{
			IEnumerable<PersonFull> result = null;
			try
			{
				result = this.channel.GetPersonList(ApplicationContext.Current.Val.Id, idList);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetPersonList", "Error getting person list", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}

			return result;
		}

		public IList<PersonHandelseLogg> GetPersonHandelseLoggByPersonId(Guid id)
		{
			IList<PersonHandelseLogg> result = null;
			try
			{
				result = this.channel.GetPersonHandelseLoggByPersonId(ApplicationContext.Current.Val.Id, id);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetPersonHandelseLoggByPersonId", "Error getting personhandelselogg by personid", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}

			return result;
		}

		public PersonHandelseLogg GetPersonHandelseLogg(Guid id)
		{
			PersonHandelseLogg result = null;
			try
			{
				result = this.channel.GetPersonHandelseLogg(ApplicationContext.Current.Val.Id, id);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetPersonHandelseLogg", "Error getting personhandelselogg", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}

			return result;
		}

		public void CreateHandelseLogg(PersonHandelseLogg b)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.CreatePersonHandelseLogg(b);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "CreateHandelseLogg", "Error creating personhandelselogg", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}
		}

		public void UpdateHandelseLogg(PersonHandelseLogg bhl)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.UpdatePersonHandelseLogg(bhl);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "UpdateHandelseLogg", "Error updating personhandelselogg", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}
		}

		public void DeleteHandelseLogg(Guid id)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.DeletePersonHandelseLogg(ApplicationContext.Current.Val.Id, id);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "DeleteHandelseLogg", "Error deleting personhandelselogg", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}
		}

		public void UpdatePerson(Person person)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.UpdatePerson(person);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "UpdatePerson", "Error updating person", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}
		}

		public Guid CreatePerson(Person person)
		{
			base.CheckWriteAllowed();

			Guid result = Guid.Empty;
			try
			{
				result = this.channel.CreatePerson(person);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "CreatePerson", "Error creating person", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}
			return result;
		}

		public Byggnad[] GetByggnadListByKontaktpersonId(Guid id)
		{
			Byggnad[] result = null;
			try
			{
				result = this.channel.GetByggnadListByKontaktpersonId(ApplicationContext.Current.Val.Id, id);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetByggnadListByKontaktpersonId", "Error getting byggnader by kontaktperson", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}
			return result;
		}

		public void AddKontaktpersonToByggnad(PersonByggnad pb)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.AddKontaktpersonToByggnad(pb);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "AddKontaktpersonToByggnad", "Error adding kontaktperson to byggnad", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}
		}

		public void RemoveKontaktpersonFromByggnad(Guid personId, Guid byggnadId)
		{
			base.CheckWriteAllowed();

			try
			{
				this.channel.RemoveKontaktpersonFromByggnad(ApplicationContext.Current.Val.Id, personId, byggnadId);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "RemoveKontaktpersonFromByggnad", "Error removing kontaktperson from byggnad", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}
		}

		public void CreatePersonPersonTyp(Guid personId, Guid persontypId)
		{
			try
			{
				this.channel.CreatePersonPersonTyp(ApplicationContext.Current.Val.Id, personId, persontypId);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "CreatePersonPersonTyp", "Error creating personpersontyp", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}
		}

		public void UpdatePersonList(IEnumerable<Person> personer)
		{
			try
			{
				this.channel.UpdatePersonList(ApplicationContext.Current.Val.Id, personer);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "UpdatePersonList", "Error updating list of persons", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}
		}

		public IList<Person> GetPersonListByPersonnummer(IEnumerable<string> personnummer)
		{
			IList<Person> result = null;
			try
			{
				result = this.channel.GetPersonListByPersonnummer(ApplicationContext.Current.Val.Id, personnummer);
			}
			catch (FaultException<ErrorInformation> ex)
			{
				Logger.LogError(this, LayerNames.Services, "GetPersonListByPersonnummer", "Error getting list of persons by personnummer", ex);
				throw new ApplicationException(ex.Detail.ServiceError);
			}
			return result;
		}
	}
}
